package com.ruoyi.kenuo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.RsaUtilClient;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.kenuo.domain.Messages;
import com.ruoyi.kenuo.service.IMessagesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消息中心Controller
 *
 * @author polo tang
 * @date 2023-09-14
 */
@RestController
@RequestMapping("/kenuo/messages")
public class MessagesController extends BaseController
{
    @Autowired
    private IMessagesService messagesService;
    @Autowired
    private ISysUserService userService;
    /**
     * 查询消息中心列表
     */
   // @PreAuthorize("@ss.hasPermi('kenuo:messages:list')")
    @GetMapping("/list")
    public TableDataInfo list(Messages messages) throws Exception {
        //解密
        String doctorName = messages.getDoctorName();
        if(StringUtils.isNotEmpty(doctorName)){
            doctorName = RsaUtilClient.decryptByPrivateKeyTwo("",doctorName);
            messages.setDoctorName(doctorName);
        }
        String clientName = messages.getClientName();
        if(StringUtils.isNotEmpty(clientName)){
            clientName = RsaUtilClient.decryptByPrivateKeyTwo("",clientName);
            messages.setClientName(clientName);
        }
        startPage();
        List<Messages> list = messagesService.selectMessagesList(messages);
        list.stream().forEach(x->{
            String cname = x.getClientName();
            String dname = x.getDoctorName();
            SysUser clientuser = userService.getUserByUserName(cname);
            SysUser docuser = userService.getUserByUserName(dname);
            x.setClientUrl(clientuser.getAvatar());
            x.setDocUrl(docuser.getAvatar());
            //清除未读信息
            if(messages.getReadFlag()==1){
                messagesService.updateByName(dname,cname,"1",x.getId());
            }
            if(messages.getReadFlag()==2){
                messagesService.updateByName(dname,cname,"2",x.getId());
            }

            //加密
            if(StringUtils.isNotEmpty(dname)){
                try {
                    dname = RsaUtilClient.encryptByPublicKeyTwo(dname);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                x.setDoctorName(dname);
            }
            if(StringUtils.isNotEmpty(cname)){
                try {
                    cname = RsaUtilClient.encryptByPublicKeyTwo(cname);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                x.setClientName(cname);
            }

        });
        return getDataTable(list);
    }
    @GetMapping("/getMessList")
    public TableDataInfo getMessList(Messages messages)
    {
        startPage();
        List<Messages> list = messagesService.getMessList(messages);
        list.stream().forEach(x->{
            String cname = x.getClientName();
            String dname = x.getDoctorName();
            SysUser clientuser = userService.getUserByUserName(cname);
            SysUser docuser = userService.getUserByUserName(dname);
            x.setClientUrl(clientuser.getAvatar());
            x.setDocUrl(docuser.getAvatar());
            //清除未读信息
            if(messages.getReadFlag()==1){
                messagesService.updateByName(dname,cname,"1",x.getId());
            }
            if(messages.getReadFlag()==2){
                messagesService.updateByName(dname,cname,"2",x.getId());
            }

        });
        return getDataTable(list);
    }
    /**
     * 导出消息中心列表
     */
    @PreAuthorize("@ss.hasPermi('kenuo:messages:export')")
    @Log(title = "消息中心", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Messages messages)
    {
        List<Messages> list = messagesService.selectMessagesList(messages);
        ExcelUtil<Messages> util = new ExcelUtil<Messages>(Messages.class);
        util.exportExcel(response, list, "消息中心数据");
    }

    /**
     * 获取消息中心详细信息
     */
    @PreAuthorize("@ss.hasPermi('kenuo:messages:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(messagesService.selectMessagesById(id));
    }

    /**
     * 新增消息中心
     */
    //@PreAuthorize("@ss.hasPermi('kenuo:messages:add')")
    @Log(title = "消息中心", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Messages messages) throws Exception {
        //解密
        String doctorName = messages.getDoctorName();
        if(StringUtils.isNotEmpty(doctorName)){
            doctorName = RsaUtilClient.decryptByPrivateKeyTwo("",doctorName);
            messages.setDoctorName(doctorName);
        }else{
            return AjaxResult.error("doctorUname is null");
        }
        String clientName = messages.getClientName();
        if(StringUtils.isNotEmpty(clientName)){
            clientName = RsaUtilClient.decryptByPrivateKeyTwo("",clientName);
            messages.setClientName(clientName);
        }
        return toAjax(messagesService.insertMessages(messages));
    }

    /**
     * 修改消息中心
     */
    @PreAuthorize("@ss.hasPermi('kenuo:messages:edit')")
    @Log(title = "消息中心", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Messages messages) throws Exception {
        return toAjax(messagesService.updateMessages(messages));
    }

    /**
     * 删除消息中心
     */
    @PreAuthorize("@ss.hasPermi('kenuo:messages:remove')")
    @Log(title = "消息中心", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(messagesService.deleteMessagesByIds(ids));
    }
}
